﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptCoreLib;
using ScriptCoreLib.JavaScript.Extensions;
using ScriptCoreLib.JavaScript.DOM.HTML;
using ScriptCoreLib.Shared.Lambda;
//using ScriptCoreLib.Shared.Avalon.Extensions;

namespace AvalonTycoonMansion.JavaScript
{
	using TargetCanvas = global::AvalonTycoonMansion.Shared.OrcasAvalonApplicationCanvas;
	using AvalonTycoonMansion.Shared;

	[Script]
	[ScriptApplicationEntryPoint(
		Width = TargetCanvas.DefaultWidth,
		Height = TargetCanvas.DefaultHeight,
		Background = true,
		BackgroundColor = 0,
		AlignToCenter = true
	)]
	public class OrcasAvalonApplicationDocument
	{
		public OrcasAvalonApplicationDocument(IHTMLElement e)
		{
			// wpf here
			var clip = new IHTMLDiv();

			clip.style.color = "white";
			clip.style.backgroundColor = "black";


			clip.style.position = ScriptCoreLib.JavaScript.DOM.IStyle.PositionEnum.relative;
			clip.style.SetSize(TargetCanvas.DefaultWidth, TargetCanvas.DefaultHeight);
			clip.style.overflow = ScriptCoreLib.JavaScript.DOM.IStyle.OverflowEnum.hidden;


			if (e == null)
				clip.AttachToDocument();
			else
				e.insertPreviousSibling(clip);

			new IHTMLImage("assets/AvalonTycoonMansion/preview.jpg").InvokeOnComplete(
				preview =>
				{
					preview.ToBackground(clip);

					//preview.style.position = ScriptCoreLib.JavaScript.DOM.IStyle.PositionEnum.absolute;
					//preview.style.left = "0px";
					//preview.style.top = "0px";
					//preview.AttachTo(clip);

					var loading = new IHTMLElement(IHTMLElement.HTMLElementEnum.h3);

					loading.style.marginTop = "10px";
					loading.style.marginLeft = "10px";

					loading.AttachTo(clip);

					var bar = new IHTMLDiv();

					bar.style.marginTop = "10px";
					bar.style.marginLeft = "10px";

					bar.style.backgroundColor = "blue";
					bar.style.height = "4px";
					bar.AttachTo(clip);

					var Images = KnownAssets.Default.FileNames.Where(k => k.EndsWith(".jpg") || k.EndsWith(".png")).ToArray();

					Images.ForEach(
						(string src, int index, Action SignalNext) =>
						{
							IHTMLImage img = src;

							var c = Images.Length;

							var p = ((index + 1) * 100 / c);

							bar.style.width = ((TargetCanvas.DefaultWidth - 20) * p / 100) + "px";

							if (Images.Length - 1 == index)
								ScriptCoreLib.JavaScript.Native.Document.title = "loading...";
							else
                                ScriptCoreLib.JavaScript.Native.Document.title = "downloading... " + p + "% " + src;

							img.InvokeOnComplete(_img => SignalNext(), 5);
						}
					)(
						delegate
						{
                            ScriptCoreLib.Shared.Avalon.Extensions.AvalonSharedExtensions
						.AtDelay(1,
								delegate
								{
									bar.Orphanize();
									loading.Orphanize();
                                    ScriptCoreLib.JavaScript.Native.Document.title = "one moment...";

									var c = new TargetCanvas();

                                    c.ToFullscreen =
                                       delegate
                                       {
                                           c.ToHTMLElement().requestFullscreen();
                                       };

									ScriptCoreLib.JavaScript.Extensions.AvalonExtensions.AttachToContainer(c, clip);
								}
							);
						}
					);

				}
			);
		}

		static OrcasAvalonApplicationDocument()
		{
			typeof(OrcasAvalonApplicationDocument).SpawnTo(i => new OrcasAvalonApplicationDocument(i));
		}

	}
}
